package com.felton.springbootflyway.config;

import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.List;

@Configuration
public class FlywayConfig {
    @Autowired 
    private DataSource dataSource;

    @Value("${spring.datasource.flyway.locations}")
    private String[] locations;

    @PostConstruct
    public void migrate() {
        Flyway flyway = Flyway.configure()
                .dataSource(dataSource)
                .locations(locations)
                .baselineOnMigrate(true)
                .load();

        flyway.repair();
        flyway.migrate();

    }
}
